package com.promis.party.ui;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.KeyEvent;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.Toast;

import com.promis.compat.StatusBarCompat;
import com.promis.party.R;
import com.promis.party.ui.home.HomeFragment;
import com.promis.party.ui.learneducation.LearnEducationFragment;
import com.promis.party.ui.partyactivity.PartyActivityFragment;
import com.promis.party.ui.partywork.PartyWorkManagerFragment;
import com.promis.util.ActivityUtils;
import com.promis.util.ColorUtils;
import com.promis.util.DrawableUtils;
import com.promis.util.ScreenUtils;
import com.promis.widget.FragmentTabHost;

public class MainActivity extends AppCompatActivity {
    private String[] rdoTexts = new String[]{"党员主页", "党务管理", "党内活动", "学习教育"};

    private int[] rdoPressedResIds = new int[]{R.mipmap.activity_main_home_pressed,
            R.mipmap.activity_main_work_pressed, R.mipmap.activity_main_activity_pressed,
            R.mipmap.activity_main_learn_pressed};

    private int[] rdoNormalResIds = new int[]{R.mipmap.activity_main_home_normal,
            R.mipmap.activity_main_work_normal, R.mipmap.activity_main_activity_normal,
            R.mipmap.activity_main_learn_normal};

    private Class[] fragments = new Class[]{HomeFragment.class,
            PartyWorkManagerFragment.class, PartyActivityFragment.class,
            LearnEducationFragment.class
    };

    private Button[] rdoBtns;
    private FragmentTabHost tabHost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarCompat.translucentStatusBar(this);
        StatusBarCompat.blackTextStatusBar(this);
        setContentView(R.layout.activity_main);

        //实例化TabHost对象，得到TabHost
        tabHost = findViewById(R.id.activity_main_tab_host);
        tabHost.setup(this, getSupportFragmentManager(), R.id.activity_main_content);

        initData();
        initListener();
    }

    private void initData() {
        rdoBtns = new Button[rdoTexts.length];
        for (int i = 0; i < rdoTexts.length; i++) {
            rdoBtns[i] = getTabButton(rdoTexts[i], rdoPressedResIds[i], rdoNormalResIds[i]);
            if (i == 0) rdoBtns[i].setSelected(true);
            //为每一个Tab按钮设置图标、文字和内容
            TabHost.TabSpec tabSpec = null;
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.DONUT) {
                tabSpec = tabHost.newTabSpec(String.valueOf(i)).setIndicator(rdoBtns[i]);
            } else {
                Drawable pressedDrawable = DrawableUtils.getDrawable(getBaseContext(), rdoPressedResIds[i]);
                Drawable normalDrawable = DrawableUtils.getDrawable(getBaseContext(), rdoNormalResIds[i]);

                Drawable drawable = DrawableUtils.getStateListDrawable(getBaseContext(), android.R.attr.state_selected, pressedDrawable, normalDrawable);
                tabSpec = tabHost.newTabSpec(String.valueOf(i)).setIndicator(rdoTexts[i], drawable);
            }
            //将Tab按钮添加进Tab选项卡中
            tabHost.getTabWidget().setDividerDrawable(null);
            tabHost.addTab(tabSpec, fragments[i], null);
        }
    }

    private void initListener() {
    }

    private Button getTabButton(String text, int pressedResId, int normalResId) {
        Button button = new Button(getBaseContext());
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        layoutParams.weight = 1;
        layoutParams.gravity = Gravity.CENTER;
        button.setPadding(0, 40, 0, 0);
        button.setLayoutParams(layoutParams);
        button.setText(text);
        button.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
        button.setBackgroundColor(ColorUtils.getColor(getBaseContext(), R.color.bg_transparent));

        button.setTextColor(ColorUtils.getColorStateList(getBaseContext(), android.R.attr.state_selected, R.color.text_black_shallow, R.color.text_red));
        button.setGravity(Gravity.CENTER);

        int px = ScreenUtils.dp2px(getBaseContext(), 30);
        Drawable pressedDrawable = DrawableUtils.getDrawable(getBaseContext(), pressedResId);
        Drawable normalDrawable = DrawableUtils.getDrawable(getBaseContext(), normalResId);

        Drawable drawable = DrawableUtils.getStateListDrawable(getBaseContext(), android.R.attr.state_selected, pressedDrawable, normalDrawable);
        drawable.setBounds(0, 0, px, px);
        button.setCompoundDrawables(null, drawable, null, null);
        button.setCompoundDrawablePadding(4);
        return button;
    }

    private long exitTime;
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                ActivityUtils.goBackstage(this);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
        }
    }
}
